﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorSQL.BLL;
using GestorSQL.BusinessEntities;
using System.Threading;
using GestorSQL.Security.Interface;
using GestorSQL.Utility.Enumeration;

namespace GestorSQL.GUI
{
    public partial class OpenCaseWindow : Form
    {
        private CaseManager _caseManager = new CaseManager();
       
        private List<Caso> _casos = new List<Caso>();

        public string CaseNumber { get; private set; }

        public OpenCaseWindow()
        {
            InitializeComponent();

            lsv_Casos.Items.Clear();

            CustomPrincipal pr = (CustomPrincipal)Thread.CurrentPrincipal;
            

            _casos = _caseManager.GetCaseList(pr.Identity.Name, pr.Roles);

            foreach (Caso caso in _casos)
            {
                string grDesc = caso.Historia.Estado_.GetDescription();
                string grName = caso.Historia.Estado_.ToString();
                ListViewGroup group = new ListViewGroup(grName, grDesc);

                if (!lsv_Casos.Groups.Contains(group))
                    lsv_Casos.Groups.Add(new ListViewGroup(grName, grDesc));

                ListViewItem item = new ListViewItem()
                {
                    Text = caso.NumeroCaso,
                    ToolTipText = caso.NumeroCaso,
                    Group = lsv_Casos.Groups[grName],
                    ImageIndex = 0
                };

                lsv_Casos.Items.Add(item);

            }
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AbrirCaso(object sender, EventArgs e)
        {
            if (!ValidateForm())
                return;
            CaseNumber = lsv_Casos.FocusedItem.Text;



            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool ValidateForm()
        {
            if (lsv_Casos.SelectedItems.Count > 0)
                return true;
            return false;
        }

        private void lsv_Casos_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                string caseNumber = e.Item.Text;
                Caso caso = _casos.FirstOrDefault(i => i.NumeroCaso.Equals(caseNumber));
                txt_Analista.Text = caso.UsuarioPruebas;
                txt_Dueno.Text = caso.UsuarioDueno;
                txt_UltimoEstado.Text = caso.Historia.Estado_.GetDescription();
                txt_FechaUltimaModificacion.Text = caso.Historia.Fecha.ToString();
                txt_Descripcion.Text = caso.Historia.Descripcion;
            }
        }

        private void OpenCaseWindow_Load(object sender, EventArgs e)
        {

        }


    }
}
